var game = new Phaser.Game(800,600,Phaser.Canvas,'',{preload:preload,create:create,update:update});

function preload() {
    game.load.image('sky', 'assets/sky.png');
    game.load.image('ground', 'assets/platform.png');
    game.load.image('star', 'assets/star.png');
    game.load.image('grass', 'assets/grass.png');
    game.load.image('ice', 'assets/ice.png');
    game.load.spritesheet('dude', 'assets/dude.png', 32, 48);

}

var player;
var platforms;
var cursors;

var stars;
var score = 0;
var scoreText;

function create() {



    game.physics.startSystem(Phaser.Physics.ARCADE);

    game.add.sprite(0, 0, 'sky');

    platforms = game.add.group();

    platforms.enableBody = true;

    var ground = platforms.create(0, game.world.height - 64, 'ground');

    ground.scale.setTo(2);

    ground.body.immovable = true;

    var ledge = platforms.create(500, 400, 'grass');
    ledge.body.immovable = true;

    ledge = platforms.create(400, 100, 'ice');
    ledge.body.immovable = true;

    ledge = platforms.create(150, 250, 'ice');
    ledge.body.immovable = true;

    ledge = platforms.create(350, 350, 'grass');
    ledge.body.immovable = true;

    ledge = platforms.create(600, 250, 'ice');
    ledge.body.immovable = true;

    ledge = platforms.create(50, 50, 'grass');
    ledge.body.immovable = true;

    player = game.add.sprite(100, game.world.height - 130, 'dude');

    game.physics.arcade.enable(player);

    player.body.bounce.y = 0.1;
    player.body.gravity.y = 250;
    player.body.collideWorldBounds = true;



    player.animations.add('left', [0, 1, 2, 3], 10, true);
    player.animations.add('right', [5, 6, 7, 8], 10, true);

    stars = game.add.group();

    stars.enableBody = true;


    for (var x = 150; x < 240; x+=30)
    {
        var star = stars.create(x, 150, 'star');
        
        star.body.gravity.y = 600;

        star.body.bounce.y = 0.4 + Math.random() * 0.2;
    }
        
    for (var x = 400; x < 490; x+=30)
    {
        var star = stars.create(x, 0, 'star');
        
        star.body.gravity.y = 600;

        star.body.bounce.y = 0.4 + Math.random() * 0.2;
    }   
        
    for (var x = 600; x < 690; x+=30)
    {
        var star = stars.create(x, 200, 'star');
        
        star.body.gravity.y = 600;

        star.body.bounce.y = 0.4 + Math.random() * 0.2;
    } 

    var star = stars.create(Math.random()*800, Math.random()*400, 'star');
    var star = stars.create(Math.random()*800, Math.random()*400, 'star');
    var star = stars.create(Math.random()*800, Math.random()*400, 'star');
    var star = stars.create(Math.random()*800, Math.random()*400, 'star');
    var star = stars.create(Math.random()*800, Math.random()*400, 'star');
    var star = stars.create(Math.random()*800, Math.random()*400, 'star');
    var star = stars.create(Math.random()*800, Math.random()*400, 'star');


        
    

    scoreText = game.add.text(16, 16, 'Score: 0', { fontSize: '32px', fill: '#000' });

    cursors = game.input.keyboard.createCursorKeys();
    
}

function update() {

    game.physics.arcade.collide(player, platforms);
    game.physics.arcade.collide(stars, platforms);

    game.physics.arcade.overlap(player, stars, collectStar, null, this);

    player.body.velocity.x = 0;

    if (cursors.left.isDown)
    {
        player.body.velocity.x = -200;

        player.animations.play('left');
    }
    else if (cursors.right.isDown)
    {
        player.body.velocity.x = 200;

        player.animations.play('right');
    }
    else
    {
        player.animations.stop();

        player.frame = 4;
    }
    
    if (cursors.up.isDown && player.body.touching.down)
    {
        player.body.velocity.y = -350;
    }
    if (cursors.down.isDown)
    {
        player.body.velocity.y = 400;
    }
}

function collectStar (player, star) {
    
    star.kill();
    score += 0.625;
    scoreText.text = 'Score: ' + score;

    if (score%10==0) {
            for (var x = 0; x < 800; x+=50)
    {
        var star = stars.create(x, 0, 'star');
        
        star.body.gravity.y = 600;

        star.body.bounce.y = 0.4 + Math.random() * 0.2;
    }
    }
    if (score%20==0) {
        var star = stars.create(Math.random()*800, Math.random()*400, 'star');
        
        star.body.gravity.y = 600;

        star.body.bounce.y = 1;
    }

    if (score%40==0) {
        var star = stars.create(Math.random()*800, Math.random()*400, 'star');
        
        star.body.gravity.y = 600;

        star.body.bounce.y = 1;
    }
    if (score%80==0) {
        var star = stars.create(Math.random()*800, Math.random()*400, 'star');
        
        star.body.gravity.y = 600;

        star.body.bounce.y = 1;
    }
    if(score>160){
        game.add.text(Math.random()*600, Math.random()*400, '你竟然玩了这么多！！', { fontSize: '1024px', fill: '#ff0000' });
    }



}